package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.common.exception.ServiceException;
import cn.js.sangu.unitStore.dto.ResetPwdDTO;
import cn.js.sangu.unitStore.dto.UserDTO;
import cn.js.sangu.unitStore.entity.User;
import cn.js.sangu.unitStore.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 说明：
 * 作者：linshuo
 * 日期：2024/8/8 10:04
 */
@RestController
@RequestMapping("login")
public class LoginController {

    static final String OPERATION_LOGIN = "登录操作";
    static final String OPERATION_REGISTER = "注册操作";
    static final String OPERATION_RESET = "重置操作";

    @Resource
    UserService userService;

    @MyLogsAOP(operation = OPERATION_LOGIN,operaType = LogType.LOGIN)
    @PostMapping("login")
    public R login(@RequestBody UserDTO dto){
        if (StrUtil.isBlank(dto.getUsername()) || StrUtil.isBlank(dto.getPassword())){
            return R.fail("用户名或密码不完整");
        }
        return R.ok(userService.login(dto));
    }

    @MyAuthAccess
    @MyLogsAOP(operation = OPERATION_LOGIN,operaType = LogType.LOGIN)
    @GetMapping("login-test")
    public R loginTest(){
        UserDTO dto = new UserDTO();
        dto.setUsername("111");
        dto.setPassword("111");
        return R.ok(userService.login(dto));
    }

    @MyLogsAOP(operation = OPERATION_REGISTER,operaType = LogType.REGISTER)
    @PostMapping("register")
    public R register(@RequestBody UserDTO dto){
        if (StrUtil.isBlank(dto.getUsername()) || StrUtil.isBlank(dto.getPassword())){
            throw new ServiceException("用户名或密码不完整");
        }
        if (StrUtil.isBlank(dto.getRole())){
            dto.setRole("用户");
        }
        if (StrUtil.isBlank(dto.getSmsCode())){
            throw new ServiceException("请填入短信验证码");
        }
        //暂时的通道
        if ("121212".equals(dto.getSmsCode())){
            User user = userService.register(dto);
            BeanUtils.copyProperties(user,dto);
            return R.ok(dto);
        }else {
            throw new ServiceException("短信验证码不正确");
        }
    }

    /**
     * 忘记密码0、重置密码1
     * @param dto
     * @return
     */
    @MyAuthAccess
    @MyLogsAOP(operation = OPERATION_RESET,operaType = LogType.RESET)
    @PutMapping("forgetPwd")
    public R forgetPwd(@RequestBody ResetPwdDTO dto){
        userService.forgetPwd(dto);
        return R.ok("重置密码成功");
    }

    /**
     * 获取短信验证码
     * @param
     * @return
     */
    @MyAuthAccess
    @MyLogsAOP(operation = OPERATION_REGISTER,operaType = LogType.SMSCODE)
    @PostMapping("getSmsCode")
    public R getSmsCode(@RequestBody UserDTO dto){
        //调用68api接口检查是否安全发送短信验证码
//        Map<String,Object> param = new HashMap<>();
//        param.put("token",dto.getApiToken());
//        param.put("appKey","95AAA9619DCB047B");
//        param.put("retry",1);
//        String res = HttpUtil.post("https://68api.com/captcha/check", param); //{"code":1,"check_time":1738906652489,"msg":"验证通过"}
//        JSONObject obj = JSONUtil.parseObj(res);
//        if (1 == Integer.parseInt(obj.get("code").toString())){ //code=1校验成功，code=0校验失败
//            System.out.println(dto.getPhone()+"准备发送短信验证码");
//            return R.ok(dto.getPhone()+"短信验证码发送成功");
//        }else {
//            throw new ServiceException("图形验证码："+obj.get("msg").toString());
//        }
//        return R.ok("当前是开发阶段，并未向"+dto.getPhone()+"发送短信验证码");
        return R.ok("验证码功能受限，并未向"+dto.getPhone()+"发送短信验证码，可联系管理员");
    }

}
